home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_Tix.idb / usr / freeware / lib / tix4.1 / demos / samples / Control.tcl.z / Control.tcl
Encoding:
Text File  |  1999-01-26  |  3.3 KB  |  130 lines

  1. # Tix Demostration Program
  2. #
  3. # This sample program is structured in such a way so that it can be
  4. # executed from the Tix demo program "widget": it must have a
  5. # procedure called "RunSample". It should also have the "if" statment
  6. # at the end of this file so that it can be run as a standalone
  7. # program using tixwish.
  8.  
  9. # This file demonstrates the use of the tixControl widget -- it is an
  10. # entry widget with up/down arrow buttons. You can use the arrow buttons
  11. # to adjust the value inside the entry widget.
  12. #
  13. # This example program uses three Control widgets. One lets you select
  14. # integer values; one lets you select floating point values and the last
  15. # one lets you select a few names.
  16. #
  17. proc RunSample {w} {
  18.  
  19.     # Create the tixControls on the top of the dialog box
  20.     #
  21.     frame $w.top -border 1 -relief raised
  22.  
  23.     # $w.top.a allows only integer values
  24.     #
  25.     # [Hint] The -options switch sets the options of the subwidgets.
  26.     # [Hint] We set the label.width subwidget option of the Controls to 
  27.     #        be 16 so that their labels appear to be aligned.
  28.     #
  29.     global demo_maker demo_thrust demo_num_engins
  30.     set demo_maker    P&W
  31.     set demo_thrust    20000.0
  32.     set demo_num_engins 2
  33.  
  34.  
  35.     tixControl $w.top.a -label "Number of Engines: " -integer true \
  36.     -variable demo_num_engins -min 1 -max 4\
  37.     -options {
  38.         entry.width 10
  39.         label.width 20
  40.         label.anchor e
  41.     }
  42.  
  43.     tixControl $w.top.b -label "Thrust: " -integer false \
  44.     -min 10000.0 -max 60000.0 -step 500\
  45.     -variable demo_thrust \
  46.     -options {
  47.         entry.width 10
  48.         label.width 20
  49.         label.anchor e
  50.     }
  51.  
  52.     tixControl $w.top.c -label "Engin Maker: " \
  53.     -incrcmd "ctl:adjust_maker $w.top.c +1" \
  54.     -decrcmd "ctl:adjust_maker $w.top.c -1" \
  55.     -validatecmd "ctl:validate_maker $w.top.c" \
  56.     -value "P&W" \
  57.     -options {
  58.         entry.width 10
  59.         label.width 20
  60.         label.anchor e
  61.     }
  62.  
  63.     pack $w.top.a $w.top.b $w.top.c -side top -anchor w
  64.  
  65.     # Use a ButtonBox to hold the buttons.
  66.     #
  67.     tixButtonBox $w.box -orientation horizontal
  68.     $w.box add ok     -text Ok     -underline 0 -command "ctl:okcmd $w" \
  69.     -width 6
  70.     $w.box add cancel -text Cancel -underline 0 -command "destroy $w" \
  71.     -width 6
  72.  
  73.     pack $w.box -side bottom -fill x
  74.     pack $w.top -side top -fill both -expand yes
  75. }
  76.  
  77. set ctl_makers {GE P&W "Rolls Royce"}
  78.  
  79. # This procedure gets called when the user presses the up/down arrow buttons.
  80. # We return the "previous" or "next" engin maker according to the "$by"
  81. # argument
  82. #
  83. proc ctl:adjust_maker {w by value} {
  84.     global ctl_makers
  85.  
  86.     set index [lsearch $ctl_makers $value]
  87.     set len   [llength $ctl_makers]
  88.     set index [expr $index $by]
  89.            
  90.     if {$index < 0} {
  91.     set index [expr $len -1]
  92.     }
  93.     if {$index >= $len} {
  94.     set index 0
  95.     }
  96.  
  97.     return [lindex $ctl_makers $index]
  98. }
  99.  
  100. proc ctl:validate_maker {w value} {
  101.     global ctl_makers
  102.  
  103.     if {[lsearch $ctl_makers $value] == -1} {
  104.     return [lindex $ctl_makers 0]
  105.     } else {
  106.     return $value
  107.     }
  108. }
  109.  
  110. proc ctl:okcmd {w} {
  111.     global demo_maker demo_thrust demo_num_engins
  112.  
  113.     puts "You selected $demo_num_engins engin(s) of thrust $demo_thrust made \
  114. by $demo_maker"
  115.  
  116.     destroy $w
  117. }
  118.  
  119.  
  120. # This "if" statement makes it possible to run this script file inside or
  121. # outside of the main demo program "widget".
  122. #
  123. if {![info exists tix_demo_running]} {
  124.     wm withdraw .
  125.     set w .demo
  126.     toplevel $w
  127.     RunSample $w
  128.     bind $w <Destroy> exit
  129. }
  130.